package controller

import (
	"gin/dao"
	"gin/tool"
	"github.com/gin-gonic/gin"
	"math"
	"strconv"
)

type GoodsController struct {
}

func (goods *GoodsController) Router(engine *gin.Engine) {
	engine.GET("/api/goodsList", goods.GoodsList)
}
func (goods *GoodsController) GoodsList(c *gin.Context) {
	page := c.DefaultQuery("page", "1")
	cateId, existCateId := c.GetQuery("cateId")
	if !existCateId {
		tool.Error(c, "参数解析失败")
		return
	}
	pageSize := 10
	Goods := dao.GoodsDao{Orm: tool.DbEngine}
	total := Goods.GoodsTotal("goods_cate_id", "1")            //查询出来所有数据
	totalPage := math.Ceil(float64(total) / float64(pageSize)) //查询出来一共多少页
	num, _ := strconv.Atoi(page)
	if num > int(totalPage) {
		tool.Error(c, "该页码数据不存在")
		return
	}
	goodsData := Goods.GoodsCateList(num, cateId, pageSize)
	tool.Success(c, goodsData)
}
